<?php

/**
 * Joomla! 1.5 component arts_curriculum
 * @package    arts_curriculum
 * @subpackage
 * @author     Chris Borkowski {@link http://www.guggenheim.org}
 * @author     Created on 31-August-2009
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.controller' );
require_once( JPATH_COMPONENT.DS.'helpers'.DS.'helper.php' );
/**
 * Arts_curriculum Controller
 *
 * @package Arts_curriculum
 */
class Arts_curriculumControllerVocab_Item extends JController
{
	function __construct()
	{
		parent::__construct();
		// Register Extra tasks
		$this->registerTask( 'add', 'display' );
		$this->registerTask( 'edit', 'display' );
	}
	
	function edit()
	{
		
		JRequest::setVar( 'hidemainmenu', 1 );
		JRequest::setVar( 'layout', 'form' );
		JRequest::setVar( 'task' , 'vocab_item');
		JRequest::setVar( 'edit', false );
		// Checkout the item
		$model = $this->getModel('vocab_item');
		$model->checkout()

		$this->display();
	}
	
	function add()
	{
		JRequest::setVar('view', 'vocab_item');
		JRequest::setVar( 'hidemainmenu', 1 );
		JRequest::setVar( 'layout', 'form' );
		JRequest::setVar( 'task' , 'vocab_item');
		JRequest::setVar( 'edit', false );
		// Checkout the item
		$model = $this->getModel('vocab_item');
		$model->checkout()

		$this->display();
	}


	function display()
	{
		$view = JRequest::getVar('view');
		
		if (!$view) {
			JRequest::setVar('view', 'vocab_item');
			JRequest::setVar( 'layout', 'form' );
		}

		parent::display();
	}
	
	
	
	
	// function display( )
// 	{
// 		switch($this->getTask())
// 		{
// 			case 'add' :
// 				{
// 					JRequest::setVar( 'hidemainmenu', 1 );
// 					JRequest::setVar( 'layout', 'form' );
// 					JRequest::setVar( 'view' , 'vocab_item');
// 					JRequest::setVar( 'edit', false );
// 					// Checkout the item
// 					$model = $this->getModel('vocab_item');
// 					$model->checkout();
// 				} break;
// 			case 'edit' :
// 				{
// 					JRequest::setVar( 'hidemainmenu', 1 );
// 					JRequest::setVar( 'layout', 'form' );
// 					JRequest::setVar( 'view' , 'vocab_item');
// 					JRequest::setVar( 'edit', true );
// 					// Checkout the item
// 					$model = $this->getModel('vocab_item');
// 					$model->checkout();
// 				} break;
// 		}
// 		parent::display();
// 	}
	function store()
	{
		$post = JRequest::get('post');
		$cid = JRequest::getVar( 'cid', array(0), 'post', 'array' );
		$post['id'] = (int)$cid[0];
		$this->id = $post['id'] ;
		
		//$post['text'] = JRequest::getVar('text', '', 'post', 'string', JREQUEST_ALLOWRAW);
		$post['main_body'] = JRequest::getVar( 'main_body', '', 'post', 'string', JREQUEST_ALLOWRAW );
		
		$model = $this->getModel('vocab_item');
		if ($model->store($post)) {
			$this->msg = JText::_( 'YO ! Item Saved');
		} else {
			$this->msg = JText::_( 'BOO ! Error Saving Item' );
		}
		

		// Now that that's all done check the table in so it can be edited.... we are done with it anyway
		$model->checkin();
	}
	
	
	function save()
	{
		$this->store() ;
		$link = 'index.php?option=com_arts_curriculum';
		$this->setRedirect( $link, $this->msg);
	}
	function apply()
	{
		$this->store() ;
		$link = 'index.php?option=com_arts_curriculum&view=vocab_item&task=edit&cid[]=' . $this->id ;
		$this->setRedirect($link, $this->msg);
	}
	function remove()
	{
		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);
		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to delete' ) );
		}
		$model = $this->getModel('item');
		if(!$model->delete($cid)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>";
		}
		$this->setRedirect( 'index.php?option=com_arts_curriculum&view=vocab' );
	}

	function publish()
	{
		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);
		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to publish' ) );
		}
		$model = $this->getModel('item');
		if(!$model->publish($cid, 1)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>";
		}
		$this->setRedirect( 'index.php?option=com_arts_curriculum&view=vocab' );
	}

	function unpublish()
	{
		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);
		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to unpublish' ) );
		}
		$model = $this->getModel('item');
		if(!$model->publish($cid, 0)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>";
		}
		$this->setRedirect( 'index.php?option=com_arts_curriculum&view=vocab' );
	}
	function cancel()
	{
		// Checkin the item
		$model = $this->getModel('item');
		$model->checkin();
		$this->setRedirect( 'index.php?option=com_arts_curriculum&view=vocab' );
	}

	function orderup()
	{
		$model = $this->getModel('item');
		$model->move(-1);
		$this->setRedirect( 'index.php?option=com_arts_curriculum&view=vocab');
	}
	function orderdown()
	{
		$model = $this->getModel('item');
		$model->move(1);
		$this->setRedirect( 'index.php?option=com_arts_curriculum&view=vocab');
	}
	function saveorder()
	{
		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		$order = JRequest::getVar( 'order', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);
		JArrayHelper::toInteger($order);
		$model = $this->getModel('item');
		$model->saveorder($cid, $order);
		$msg = 'New ordering saved';
		$this->setRedirect( 'index.php?option=com_arts_curriculum&view=vocab', $msg );
	}
	


}// the end

